﻿/*========================== 
 * @author 郑金泉
 * @desc 实体LongList字段类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;

namespace Obsidian.Edm
{
    public class LongListField : ListField, IModelField
    {

        private char separator = ',';

        public override FieldType Type
        {
            get { return FieldType.LongList; }
        }

        private IList<long> _value = new List<long>();
        /// <summary>
        /// 获取值
        /// </summary>
        public IList<long> Value
        {
            get
            {
                if (_isNull)
                    throw new Exception("空值状态无法获得该属性值");
                return this._value;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        public object OValue
        {
            get
            {
                if (_isNull) return null;
                else return _value;
            }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(IList<long> value)
        {
            if (!base._assigned)
                base._assigned = true;
            this._isNull = false;
            this._value = value;
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IModel Set(object value)
        {
            this.SetNull();
            if (value == null)
                return base.Model;

            Type type = value.GetType();
            if (type == typeof(string))
            {
                string str = Convert.ToString(value);
                long[] arr = ArrayUtil.SplitInt64(str, separator);
                if (arr.Length == 0)
                {
                    this._isNull = false;
                }
                else
                {
                    foreach (int val in arr)
                        this.Add(val);
                }
            }
            else
            {
                this.Add(Convert.ToInt64(value));
            }
            return base.Model;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(IList<long> value)
        {
            try
            {
                this.Set(value);
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 设置值
        /// </summary>
        public bool TrySet(object value)
        {
            try
            {
                this.Set(value);
                return true;
            }
            catch { return false;}
        }

        /// <summary>
        /// 设置为空值状态
        /// </summary>
        public IModel SetNull()
        {
            this.Clear();
            return base.Model;
        }


        public LongListField(IModel model)
        {
            this.InitField(model);
        }

        public LongListField(IModel model, string dbFieldName)
        {
            this.InitField(model, dbFieldName);
        }

        public LongListField(IModel model, string dbFieldName, string alias)
        {
            this.InitField(model, dbFieldName, alias);
        }

        public LongListField(IModel model, string dbFieldName, string alias, char separator)
        {
            this.InitField(model, dbFieldName, alias);
            this.separator = separator;
        }

        public void Add(long val)
        {
            this._value.Add(val);
            this._assigned = true;
            if (this._isNull == true)
                this._isNull = false;
        }

        public void Add(IEnumerable<long> vals)
        {
            foreach (long val in vals)
                this.Add(val);
        }

        public int Count
        {
            get { return this._value.Count; }
        }

        public void Clear()
        {
            this._value.Clear();
            this._assigned = true;
            this._isNull = true;
        }

        public string ToString()
        {
            return _isNull ? "" : ArrayUtil.Join<long>(_value, this.separator.ToString());
        }
    }
}
